<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-18
 * Time: 下午4:58
 */
class SupplyController extends Controller {

    private $_title;

    public function init() {
        parent::init();
    }

    public function indexAction(){
        global $_suppliyStateCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $Supply = new SupplyModel();

        $keyword= $this->_request->getParam('keyword');
        $keyword=empty($keyword) ? '' :htmlspecialchars(trim(urldecode($keyword)) , ENT_QUOTES ,'utf-8');
        $pageurl="/supply/index";

        //拼接条件
        $where ="1=1";
        if(!empty($keyword)){
            $condition[] ="users_invoice.business_name like '%{$keyword}%'";
            $pageurl .= "/keyword/".$keyword;
        }

        $condition[] ="iq_quotation.type='outer' and iq_quotation.status in({$_suppliyStateCfg['up']})";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }


        //排序
        $orderby = "iq_quotation.update_time desc";

        $totalItems = $Supply->getPageTatolWhere($where);

        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        $list = $Supply->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);
        foreach($list as $key=>$val){
            $list[$key]['update_time'] = Tools::time2Units(time () - strtotime ( $val['update_time'] )) . "前";
        }

       $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;
        $this->_view->keyword = $keyword;
        $this->_view->list = $list;
    }


    public function shoplistAction(){
        define ( 'ITEM_PER_PAGE', 20 );

        $id = $this->_request->getParam("id",'0');


        $supply = new SupplyModel();
        $supplydetail = $supply->getListById($id);

        $this->_view->supplydetail = $supplydetail;

        $quota = new QuotationModel();
        $pageurl="/supply/shoplist";

        if(!empty($id)){
            $pageurl .= "/id/".$id;
        }

        $where ="type='outer' and iq_quotation.business_id='{$id}'";
        $orderby ="update_time desc";

        $totalItems = $quota->getCountByWhere($where);

        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        $lists = $quota->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($lists as $key=>$var){
            $lists[$key]['update_time'] = Tools::time2Units(time() - strtotime($var['update_time'])) . "前";

        }


        $this->_view->lists = $lists;
        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');
        $this->_view->title = $supplydetail['business_remarks'];
        $this->_view->links = $links;
    }

    public function detailAction(){

        $this->_title = "牌号详情";

        $id = $this->_request->getParam("id",'0');

        $buy = new QuotationModel();
        $detail = $buy->getListById("iq_quotation.id=".$id);

        $detail['update_time'] = Tools::time2Units(time() - strtotime($detail['update_time'])) . "前";
        $this->_view->title = $this->_title ;
        $this->_view->detail = $detail;

    }
}